﻿#include "ais_vectorcloud.h"
#include "ais_curve.h"
#include "ais_surface.h"

CAGD::AIS_VectorCloud::AIS_VectorCloud()
{
    // 设置属性
    m_info.color = {0, 0, 1, 0.5};
}

void CAGD::AIS_VectorCloud::UpdateArray()
{
    // 首先刷新
    auto parent = GetParent();
    parent->UpdateInfo();

    auto &info = parent->GetInfo();
    switch (parent->GetType())
    {
    case AIS_Type::CURVE: {
        auto cur = dynamic_cast<AIS_Curve *>(parent);
        m_origins = GeomCurve::UniformPointSample(cur->GetCurve(), info.size);
        m_vectors = GeomCurve::UniformNormalSample(cur->GetCurve(), info.size);
        break;
    }
    case AIS_Type::SURFACE: {
        auto sur = dynamic_cast<AIS_Surface *>(parent);

        // 注意 info.size 是所有离散顶点的数量，而要显示的法向离散数量应该非常少
        int dis = sqrt(info.size) / 20;
        m_origins = GeomSurface::UniformPointSample(sur->GetSurface(), dis, dis);
        m_vectors = GeomSurface::UniformNormalSample(sur->GetSurface(), dis, dis);
        break;
    }
    }

    AIS_Base::UpdateArray();
}

void CAGD::AIS_VectorCloud::UpdateInfo()
{
    // 继承模型矩阵
    auto parent = GetParent();
    if (parent != nullptr)
    {
        parent->UpdateInfo();
        m_info.modelMat = parent->GetInfo().modelMat;
    }

    m_info.size = m_vectors.size();
}

CAGD::AIS_Type CAGD::AIS_VectorCloud::GetType() const
{
    return AIS_Type::VECTORCLOUD;
}

void CAGD::AIS_VectorCloud::RecvMessage(Message mess)
{
    // 先调用默认接收函数
    AIS_Base::RecvMessage(mess);

    switch (mess)
    {
    case CAGD::Message::SHOW_INFO: {
        m_info.status = AIS_Status::NONE;
        break;
    }
    case CAGD::Message::HIDE_INFO: {
        m_info.status = AIS_Status::HIDDEN;
        break;
    }
    default:
        break;
    }
}

std::vector<CAGD::BPnt> &CAGD::AIS_VectorCloud::GetOrigins()
{
    return m_origins;
}

std::vector<CAGD::BVec> &CAGD::AIS_VectorCloud::GetVectors()
{
    return m_vectors;
}